package lab1;

import java.lang.management.ManagementFactory;
import java.lang.management.ThreadInfo;
import java.lang.management.ThreadMXBean;

class SimThread1 extends Thread
{
	public SimThread1(String str)
	{
		super(str);
	}
                                 
	public void run()
	{
		for (int i = 0; i < 10; i++)
		{
			System.out.println(i + " " + getName());
			try {
				sleep((int)(Math.random() * 1000));
			}
			catch (InterruptedException e)
			{}
		}
		System.out.println("DONE! " + getName());
	}
	
}

class IOThread1 extends Thread{
	public void run()
	{
		while(true){
			ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
			long[] threadIds = threadMXBean.getAllThreadIds();
			ThreadInfo[] threadInfos = threadMXBean.getThreadInfo(threadIds);
			for (ThreadInfo threadInfo : threadInfos) {
				Thread.State state = threadInfo.getThreadState();
				System.out.println(threadInfo.getThreadName() + " : " + state);
			}

			try {
				sleep((int)(Math.random() * 1000));
			}catch (InterruptedException e)
			{}
		}
	}
}

public class GetThreadInfo
{
	public static void main (String[] args){
		Thread one = new SimThread1("Go to Shanghai??");
		Thread two = new SimThread1("Stay here!!");
		IOThread1 thread = new IOThread1();
		

		one.start();
		two.start();
		thread.start();
		
		try {
			one.join();
			two.join();
			thread.stop();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		
		System.out.println(one.getName()+" "+one.getState());
		System.out.println(two.getName()+" "+two.getState());
	}
}

